package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.TreeMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.ConfigModel;

import admin.data.ConfigData;
import debug.ConfigDebug;
import admin.forms.ConfigForm;

public class ConfigServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		TreeMap check = new TreeMap();
		
		//Get config name
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll("/admin/config/", ""),"UTF-8");
		
		long id = 1;
		ConfigData cData = new ConfigData();
		ConfigModel configs = cData.getConfig(id);
		if (configs == null) {
			configs = new ConfigModel();
		}
		
		request.setAttribute("form", "edit");
		request.setAttribute("check", check);
		request.setAttribute("configs", configs);
		try {
			request.getRequestDispatcher("/admin/pages/config/" + getPageJsp(name) + ".jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		//Get config name
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll("/admin/config/", ""),"UTF-8");
		
		long id = 1;
		ConfigData cData = new ConfigData();
		ConfigModel configs = cData.getConfig(id);
		if (configs == null) {
			configs = new ConfigModel();
			configs.setId(1);
		}
		
		//Check valid and get value form
		TreeMap forms = getFormValid(name, request, configs);
		configs = (ConfigModel) forms.get("config");
		TreeMap check = (TreeMap) forms.get("checkValid");
		
		if (check.size() == 0) {
			cData.insert(configs);
			request.setAttribute("success", "Update success site config");
		}
		
		request.setAttribute("form", "edit");
		request.setAttribute("check", check);
		request.setAttribute("configs", configs);
		
		try {
			request.getRequestDispatcher("/admin/pages/config/" + getPageJsp(name) + ".jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String getPageJsp(String name) {
		String page = "form_site";
		if (name.equalsIgnoreCase("payment")) {
			page = "form_payment";
		}
		
		if (name.equalsIgnoreCase("reciprocal-link")) {
			page = "form_reciprocal_link";
		}
		
		if (name.equalsIgnoreCase("layout")) {
			page = "form_layout";
		}
		
		if (name.equalsIgnoreCase("email")) {
			page = "form_email";
		}
		
		return page;
	}
	
	private TreeMap getFormValid(String name, HttpServletRequest request, ConfigModel configs) {
		ConfigForm cForm = new ConfigForm();
		TreeMap forms = new TreeMap();
		if (name.equalsIgnoreCase("payment")) {
			forms = cForm.checkValidPayment(request, configs);
		} else if (name.equalsIgnoreCase("reciprocal-link")) {
			forms = cForm.checkValidReciprocal(request, configs);
		} else if (name.equalsIgnoreCase("layout")) {
			forms = cForm.checkValidLayout(request, configs);
		} else if (name.equalsIgnoreCase("email")) {
			forms = cForm.checkValidEmail(request, configs);
		} else {
			forms = cForm.checkValidSite(request, configs);
		}
		
		return forms;
	}
}
